define(['jquery', 'bootstrap', 'userend', 'table', 'form'], function ($, undefined, Userend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/order/order/index',
                    table: 'mall_order_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'orderNo', title: __('Orderno')},
                        {field: 'orderunique', title: __('Orderunique'), visible:false},
                        {field: 'user_id', title: __('User_id'), visible:false},
                        {field: 'goodsMoney', title: __('Goodsmoney'), operate:'BETWEEN'},
                        {field: 'deliverType', title: __('Delivertype'), searchList: {"0":__('Delivertype 0'),"1":__('Delivertype 1')}, formatter: Table.api.formatter.normal},
                        {field: 'deliverMoney', title: __('Delivermoney'), operate:'BETWEEN'},
                        {field: 'totalMoney', title: __('Totalmoney'), operate:'BETWEEN'},
                        {field: 'realTotalMoney', title: __('Realtotalmoney'), operate:'BETWEEN'},
                        {field: 'payType', title: __('Paytype'),searchList: {"0":__('Paytype 0'),"1":__('Paytype 1')}, formatter: Table.api.formatter.normal},
                        {field: 'payFrom', title: __('Payfrom'), visible:false},
                        {field: 'payis', title: __('Payis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'userName', title: __('Username'), visible:false},
                        {field: 'area_ids', title: __('Area_ids'), visible:false},
                        {field: 'userAddress', title: __('Useraddress'), visible:false},
                        {field: 'userPhone', title: __('Userphone'), visible:false},
                        {field: 'orderScore', title: __('Orderscore'), visible:false},
                        {field: 'invoiceis', title: __('Invoiceis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'orderRemarks', title: __('Orderremarks'), visible:false},
                        {field: 'orderSrc', title: __('Ordersrc'), visible:false},
                        {field: 'needPay', title: __('Needpay'), operate:'BETWEEN', visible:false},
                        {field: 'refundis', title: __('Refundis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'appraiseis', title: __('Appraiseis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'cancelReason', title: __('Cancelreason'), visible:false},
                        {field: 'rejectReason', title: __('Rejectreason'), visible:false},
                        {field: 'rejectOtherReason', title: __('Rejectotherreason'), visible:false},
                        {field: 'closedis', title: __('Closedis'), formatter: Table.api.formatter.toggle, visible:false},
                        {field: 'receivetime', title: __('Receivetime'), operate:'RANGE', addclass:'datetimerange', visible:false},
                        {field: 'deliverytime', title: __('Deliverytime'), operate:'RANGE', addclass:'datetimerange', visible:false},
                        {field: 'express_id', title: __('Express_id'), visible:false},
                        {field: 'expressNo', title: __('Expressno'), visible:false},
                        {field: 'tradeNo', title: __('Tradeno'), visible:false},
                        {field: 'settlement_id', title: __('Settlement_id'), visible:false},
                        {field: 'commissionFee', title: __('Commissionfee'), operate:'BETWEEN', visible:false},
                        {field: 'prom_type', title: __('Prom_type'), searchList: {"0":__('Prom_type 0'),"4":__('Prom_type 4'),"5":__('Prom_type 5'),"6":__('Prom_type 6')}, formatter: Table.api.formatter.normal},
                        {field: 'deleted', title: __('Deleted'), searchList: {"1":__('Deleted 1'),"0":__('Deleted 0')}, formatter: Table.api.formatter.normal, visible:false},
                        {field: 'status', title: __('Status'), searchList: {"unpay":__('Unpay'),"undeliver":__('undeliver'),"unreceive":__('Unreceive'),"rejected":__('Rejected'),"finished":__('Finished'),"canceled":__('Canceled'),"unappraise":__('Unappraise')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, visible:false},
                        {field: 'operate', title: __('Operate'), table: table,events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                text: __('Detail'),
                                classname: 'btn btn-xs btn-info btn-fields btn-dialog',
                                icon: 'fa fa-list',
                                url: 'mall/order/order/detail'
                            },{
                                name: 'print',
                                text: __('Print'),
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-th-list',
                                url: 'mall/order/order/printf'
                            },{
                                name: 'money',
                                text: '改价',
                                classname: 'btn btn-xs btn-success btn-dialog',
                                icon: 'fa fa-list-ul',
                                visible: function( row){
                                    //货到付款且未发货,或在线支付且未支付，则显示修改金额
                                    return  (row.payType==1 && row.status=="unpay") || (row.payType==0 && row.status=="undeliver");
                                },
                                url: 'mall/order/order/editmoney'
                            },{
                                name: 'deliver',
                                text: __('Deliver'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-th-list',
                                visible: function( row){
                                    return row.status=="undeliver";
                                },
                                url: 'mall/order/order/deliver'
                            }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        editmoney: function () {
            Controller.api.bindevent();
        },
        deliver: function () {
            Controller.api.bindevent();
            $("#c-category1").trigger("fa.event.areaidsupdated",'edit');
        },
        detail: function () {
            Controller.api.bindevent();
            $("#c-category1").trigger("fa.event.areaidsupdated",'edit');

            var order_id = $("#c-id").val();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    detail_url: 'mall/order/order/detail/ids/'+order_id,
                    table: 'mall_order_goods',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.detail_url,
                pk: 'goods_id',
                sortName: 'goods_id',
                columns: [
                    [
                        {field: 'goods_id', title: __('ID'), visible:false},
                        {field: 'name', title: __('Name'), align:'left'},
                        {field: 'shopQQ', title: __('ShopQQ'), formatter: Controller.api.formatter.QQ},
                        {field: 'goodsimg', title: __('Goodsimg'), formatter: Table.api.formatter.image},
                        {field: 'goodsname', title: __('Goodsname'), align:'left'},
                        {field: 'goodsSpecNames', title: __('Goodsspec'), align:'left'},
                        {field: 'goodssn', title: __('Goodssn')},
                        {field: 'goodsPrice', title: __('Shopprice'), operate:'BETWEEN'},
                        {field: 'goodsNum', title: __('Num'), operate:'BETWEEN'},
                        {field: 'subtotal', title: __('Subtotal'), operate:'BETWEEN'},
                    ]
                ],
                search: false,
                commonSearch:false,
                showExport: false,
                showColumns: false,
                showToggle: false,
                pagination: false,
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                $("#c-category2").data("params", function (obj) {
                    return {custom: {pid: $("#c-category1").val()}};
                });
                $("#c-category3").data("params", function (obj) {
                    return {custom: {pid: $("#c-category2").val()}};
                });
                $("#c-category4").data("params", function (obj) {
                    return {custom: {pid: $("#c-category3").val()}};
                });

                $(document).on("change", "#c-category1", function () {
                    require(['selectpage'], function () {
                        $('#c-category2').selectPageClear();
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-area_ids").val($('#c-category1').val());
                    });
                });

                $(document).on("change", "#c-category2", function () {
                    require(['selectpage'], function () {
                        $('#c-category3').selectPageClear();
                        $('#c-category4').selectPageClear();
                        $("#c-area_ids").val( $("#c-category1").val()+","+$("#c-category2").val());
                    });
                });

                $(document).on("change", "#c-category3", function () {
                    require(['selectpage'], function () {
                        $('#c-category4').selectPageClear();
                        $("#c-area_ids").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val());
                    });
                });

                $(document).on("change", "#c-category4", function () {
                    $("#c-area_ids").val( $("#c-category1").val()+","+$("#c-category2").val()+","+$("#c-category3").val()+","+$("#c-category4").val());
                });

                $(document).one("fa.event.areaidsupdated", "#c-category1", function () {
                    var a = $("#c-area_ids").val().split(",");
                    $("#c-category1").val(a[0]);
                    $("#c-category2").val(a[1]);
                    $("#c-category3").val(a[2]);
                    $("#c-category4").val(a[3]);
                });
            },
            formatter: {
                QQ: function (value, row, index) {
                    return '<a href="tencent://message/?uin='+row.shopQQ+'&Site=QQ交谈&Menu=yes"><img border="0" style="vertical-align:middle;" src="https://wpa.qq.com/pa?p=1:'+row.shopQQ+':7" alt="QQ交谈" width="71" height="24" /></a>';
                },
                specnames: function (value, row, index) {
                    return row.goodsSpecNames.replace(new RegExp("@@_@@","g"),"<br/>");
                },
            },
        }
    };
    return Controller;
});